<template>
  <div class="my-el-able">
    <el-button type="success" class="add" @click="addEvent">新增</el-button>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="tjsj" label="创建时间" width="300">
      </el-table-column>
      <el-table-column prop="sjsj" label="事件发生时间" width="300">
      </el-table-column>
      <el-table-column prop="name" label="名称"> </el-table-column>
      <el-table-column prop="centerlongitude" label="经度" width="250">
      </el-table-column>
      <el-table-column prop="centerlatitude" label="纬度" width="250">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="updataEvent(scope.row)">
            修改
          </el-button>
          <el-button @click="delEvent(scope.row)" type="text" size="small">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="my-page"
      @current-change="handleCurrentChange"
      :current-page.sync="currentPage1"
      :page-size="pageSize"
      layout="total, prev, pager, next, jumper"
      :total="totalData"
    >
    </el-pagination>
    <el-dialog
      :visible.sync="addDialogVisible"
      width="500px"
      @close="dialogClose"
    >
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px">
        <el-form-item label="事件名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="事件发生日期" prop="sjsj">
          <el-date-picker
            v-model="form.sjsj"
            type="date"
            value-format="yyyy-MM-dd"
            format="yyyy-MM-dd"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="经度" prop="centerlongitude">
          <el-input v-model="form.centerlongitude"></el-input>
        </el-form-item>
        <el-form-item label="纬度" prop="centerlatitude">
          <el-input v-model="form.centerlatitude"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            v-if="action == 'add'"
            type="success"
            @click="commitAddEvent"
          >
            提交
          </el-button>

          <el-button
            v-if="action == 'update'"
            type="success"
            @click="commitUpdateEvent"
          >
            修改
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import api from "../api/home";
export default {
  name: "zhongdashijian",
  data() {
    var checkjingdu = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("经度不能为空"));
      }
      setTimeout(() => {
        if (isNaN(Number(value))) {
          callback(new Error("请输入数字值"));
        } else {
          if (value < -180 || value > 180) {
            callback(new Error("请填写合理的经度值"));
          } else {
            callback();
          }
        }
      }, 0);
    };
    var checkweidu = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("纬度不能为空"));
      }
      setTimeout(() => {
        if (isNaN(Number(value))) {
          callback(new Error("请输入数字值"));
        } else {
          if (value < -90 || value > 90) {
            callback(new Error("请填写合理的纬度值"));
          } else {
            callback();
          }
        }
      }, 0);
    };
    return {
      action: "add",
      form: {
        name: "",
        sjsj: "",
        centerlongitude: "",
        centerlatitude: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入事件名称", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符",
            trigger: "blur",
          },
        ],
        sjsj: [
          {
            required: true,
            message: "请选择时间",
            trigger: "change",
          },
        ],
        centerlongitude: [
          { required: true, validator: checkjingdu, trigger: "blur" },
        ],
        centerlatitude: [
          { required: true, validator: checkweidu, trigger: "blur" },
        ],
      },
      currentPage1: 0,
      pageSize: 10,
      totalData: 0,
      addDialogVisible: false,
      tableData: [],
      currentUpdateId: 0,
    };
  },
  props: {},
  mounted() {
    this.userid = this.$store.state.userInfo.userid;
    this.getEventList();
  },
  methods: {
    getEventList() {
      api.getEventList(this.currentPage1, this.pageSize).then((res) => {
        console.log(res);
        if (res.code === 200) {
          this.tableData = res.data.list;
          this.totalData = res.data.total;
        } else {
          this.$message({
            type: "info",
            message: "数据获取失败",
          });
        }
      });
    },
    delEvent(row) {
      this.$confirm("此操作将删除该事件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          api.delEvent(row.id).then((res) => {
            if (res.code === 200) {
              this.getEventList();
              this.$message({
                type: "success",
                message: res.msg,
              });
            } else {
              this.$message({
                type: "info",
                message: "删除失败",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    dialogClose() {
      this.addDialogVisible = false;
    },
    handleCurrentChange(page) {
      console.log(page);
      this.currentPage1 = page;
      this.getEventList();
    },
    updataEvent(row) {
      this.action = "update";
      this.currentUpdateId = row.id;
      this.form = {
        name: row.name,
        sjsj: row.sjsj,
        centerlongitude: row.centerlongitude,
        centerlatitude: row.centerlatitude,
      };
      this.addDialogVisible = true;
    },
    commitUpdateEvent() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let { name, sjsj, centerlongitude, centerlatitude } = this.form;
          let params = {
            id: this.currentUpdateId,
            name: name,
            sjsj,
            centerlongitude,
            centerlatitude,
            username: this.userid,
          };
          api.updateEvent(params).then((res) => {
            console.log(res);
            if (res.code === 200) {
              this.addDialogVisible = false;
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.getEventList();
            } else {
              this.$message({
                type: "info",
                message: "修改失败",
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    addEvent() {
      this.action = "add";
      this.addDialogVisible = true;
    },
    commitAddEvent() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let { name, sjsj, centerlongitude, centerlatitude } = this.form;
          let params = {
            name: name,
            sjsj,
            centerlongitude,
            centerlatitude,
            username: this.userid,
          };
          api.addEvent(params).then((res) => {
            console.log(res);
            if (res.code === 200) {
              this.addDialogVisible = false;
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.getEventList();
            } else {
              this.$message({
                type: "info",
                message: "添加失败",
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss">
@import "../styles/resetElTable.scss";
@import "../styles/resetPage.scss";
.el-dialog {
  .el-form-item__label {
    color: #fff;
  }
  .el-input__inner {
    background-color: transparent;
  }
}
</style>
<style lang="scss" scoped>
.add {
  float: right;
  margin-bottom: 20px;
  margin-right: 20px;
}
.el-button--success {
  background: #2ed97a;
  border-color: #2ed97a;
}
</style>
